﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
using System.Security.Cryptography;
using System.Net;
using System.Net.Mail;

namespace RIS
{
    public class helpers
    {
        public class con
        {
            public SqlConnection connection;
            public SqlCommand command;
        }

        public static con getConnection()
        {
            String cs = ConfigurationManager.ConnectionStrings["KillerPixel"].ToString();


            SqlConnection con = new SqlConnection();

            con.ConnectionString = cs;

            con.Open();

            SqlCommand com = new SqlCommand();
            com.Connection = con;

            con c = new con();

            c.command = com;
            c.connection = con;

            return c;


        }

        public static bool checkDbExistence(String tableName, String field, String value)
        {
            con c = getConnection();
            c.command.CommandText = "select * from [" + tableName + "] where " + field + " ='" + value + "'";

            SqlDataReader r;

            r = c.command.ExecuteReader();

            r.Read();
            if (r.HasRows) return true;
            else return false;
        }

        public static string GetSHA512(string text)
        {
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] hashValue;
            byte[] message = UE.GetBytes(text);

            SHA512Managed hashString = new SHA512Managed();
            string hex = "";

            hashValue = hashString.ComputeHash(message);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
            return hex;
        }

        public static bool Send(String toEmail, String subject, String body)
        {

            String[] settings = System.Configuration.ConfigurationManager.AppSettings["emailSettings"].Split(new char[] { '|' });

            String password = settings[3];
            String userName = settings[2];
            String smtpHost = settings[0];
            int smtpPort = int.Parse(settings[1]);
            bool enableSSL = settings[4] == "1" ? true : false;
            String fromEmail = settings[5];

            NetworkCredential loginInfo = new NetworkCredential(userName, password);
            MailMessage msg = new MailMessage();

            msg.From = new MailAddress(fromEmail);
            msg.To.Add(new MailAddress(toEmail));

            msg.Subject = subject;
            msg.Body = body;

            msg.IsBodyHtml = true;
            SmtpClient client = new SmtpClient(smtpHost, smtpPort);
            client.EnableSsl = enableSSL;
            client.UseDefaultCredentials = false;
            client.Credentials = loginInfo;

            try
            {
                client.Send(msg);
            }
            catch (SmtpException ex)
            {
                return false;
            }

            return true;
        }

        public class user 
        {
            public Int64 ID;
            public string username;
            public string password;
            public string email;
        }

    }
}